from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from hotel.models import *
from django.shortcuts import HttpResponse
import time
import datetime



# Create your views here.

def register(request):
    #try:
    if ('username' in request.POST) and ('password' in request.POST) and ('tel' in request.POST):
        if request.POST:
            print(request.POST['username'], request.POST['tel'], request.POST['password'])
            u1 = User(Uname=request.POST['username'],Utel=request.POST['tel'],Upassward=request.POST['password'])
            u1.save()

            return HttpResponseRedirect('/')
    else:
        error = r'请输入正确的信息'
        return render(request,'register.html',{'error':error})
    #except Exception as e:
    #    print(e.with_traceback)


def index(request):
    return render(request,'index.html')

def list(request):
    if request.method == 'POST':
        if ('tel' in request.POST) and ('password' in request.POST):
            if request.POST:
                user1 = User.objects.get(Utel=request.POST['tel'])
                if user1.Upassward == request.POST['password']:
                    context = {
                        'tel': user1.Utel,
                        'username': user1.Uname,
                        'password': user1.Upassward,
                        'rooms': []
                    }
                    for room in Room.objects.all().filter(Rstatus=0):
                        context['rooms'].append({
                            'room_num':room.Rnum,
                            'room_price':room.Rprice,
                            'room_type':room.Rtype
                        })
                    return render(request,'list.html',context)
            else:
                return HttpResponseRedirect('/',{'error': '请输入正确的账号信息'})

def room_oreder(request):
    if ('Utel' in request.POST) and ('Rnum' in request.POST):
        if request.POST:
            u = User.objects.filter(Utel=request.POST['Utel'])
            r = Room.objects.get(Rnum=request.POST['Rnum'])
            r.Rstatus = 1
            r.save()
            ro = RoomOrder(Rid=r,Uid=u[0],ROstatus=0,ROtime=time.strftime("%Y-%m-%d", time.localtime()))
            ro.save()
            context = {
                'Uid': u[0],
                'ROid': ro.id,
                'ROtime':ro.ROtime,
                'Rprice' : request.POST['Rprice'],
                'Rtype': request.POST['Rtype'],
                'Uname' : request.POST['Uname'],
                'Utel' : request.POST['Utel'],
                'Services' : []
            }
            Services = Service.objects.all()
            for ser in Services:
                context['Services'].append({
                    'Sid': ser.id,
                    'Sname': ser.Sname,
                    'Sprice': ser.Sprice,
                    'Sdescribe': ser.Sdescribe
                })
            return render(request, 'role.html' , context)
    return HttpResponseRedirect('list/')

def room_service_order(request):
    if request.POST:
        context = {'room_order':[],'service_order':[],'rooms':[]}
        context['room_order'].append({
            'Uname': request.POST['Uname'],
            'ROtime': str(request.POST['ROtime']),
            'Rprice': request.POST['Rprice']
        })

        for k in request.POST:
            if k in ['1','2','3','4','5','6','7','8']:
                sero = ServiceOrder(
                    Uid=User.objects.get(Uname=request.POST['Uname']),
                    Sid=Service.objects.get(id = k),
                    ROid=RoomOrder.objects.get(id=request.POST['ROid']),
                    SOstatus=0,
                    SOtime=time.strftime("%Y-%m-%d", time.localtime())
                )
                sero.save()
                context['service_order'].append({
                    'Sid': sero.Sid,
                    'ROid': sero.ROid,
                    'SOtime': sero.SOtime
                })

                for room in Room.objects.all().filter(Rstatus=0):
                    context['rooms'].append({
                        'room_num': room.Rnum,
                        'room_price': room.Rprice,
                        'room_type': room.Rtype
                    })
        return render(request, 'list.html', context)
    else:
        return HttpResponseRedirect('/list/')
